package proto3;

import com.example.proto3.*;
import com.google.protobuf.Any;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;

/**
 * 将数据写入到文件中
 */
public class TestWrite {
    public final static String CONTACTS_PATH = "src/main/java/proto3/contacts.bin";

    public static void main(String[] args) {
        // 1. 读取 contacts.bin 文件，反序列化出通讯录对象
        Contacts.Builder builder = Contacts.newBuilder();
        try {
//            // 方式1：
//            Contacts contacts = Contacts.parseFrom(new FileInputStream("src/main/java/proto3/contacts.bin"));
//            Contacts.Builder builder = contacts.toBuilder();

            // 方式2：
            builder.mergeFrom(new FileInputStream(CONTACTS_PATH));
        } catch (IOException e) {
            System.out.println("contacts.bin not find, create new file");
        }

        // 2. 添加联系人信息
        builder.addInfos(addPeopleInfo());

        // 3. 序列化通讯录，将结果写入文件
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(CONTACTS_PATH);
            builder.build().writeTo(fileOutputStream);
            fileOutputStream.close();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }


    private static PeopleInfo addPeopleInfo() {
        PeopleInfo.Builder builder = PeopleInfo.newBuilder();
        Scanner scanner = new Scanner(System.in);
        System.out.println("--------------新增联系人-------------");
        System.out.print("请输入联系人姓名：");
        String name = scanner.nextLine();
        builder.setName(name);

        System.out.print("请输入联系人年龄：");
        int age = scanner.nextInt();
        scanner.nextLine();
        builder.setAge(age);

        for (int i = 0;; i++) {
            System.out.print("请输入联系人电话" + (i+1) + "(只输⼊回⻋完成电话新增): ");
            String number = scanner.nextLine();
            if (number.isEmpty()) {
                break;
            }
            Phone.Builder phoneBuilder = Phone.newBuilder();
            phoneBuilder.setNumber(number);

            System.out.print("请输入此电话类型（1、移动电话    2、固定电话）");
            int type = scanner.nextInt();
            scanner.nextLine();
            switch (type) {
                case 1:
                    phoneBuilder.setType(PhoneType.MP);
                    break;
                case 2:
                    phoneBuilder.setType(PhoneType.TEL);
                    break;
                default:
                    System.out.println("选择错误！");
                    break;
            }

            builder.addPhone(phoneBuilder);
        }

        Address.Builder addressBuilder = Address.newBuilder();
        System.out.print("请输入联系人家庭地址：");
        String homeAddress = scanner.nextLine();
        addressBuilder.setHomeAddress(homeAddress);
        System.out.print("请输入联系人单位地址：");
        String unitAddress = scanner.nextLine();
        addressBuilder.setUnitAddress(unitAddress);
        // Address -> Any
        builder.setData(Any.pack(addressBuilder.build()));

        System.out.print("请选择要添加的其他联系方式（1、qq号   2、微信号）:");
        int otherContact = scanner.nextInt();
        scanner.nextLine();
        if (1 == otherContact) {
            System.out.print("请输入qq号：");
            String qq = scanner.nextLine();
            builder.setQq(qq);
        } else if (2 == otherContact) {
            System.out.print("请输入微信号：");
            String wechat = scanner.nextLine();
            builder.setWechat(wechat);
        } else {
            System.out.println("无效选择，设置失败！");
        }

        for (int i = 0; ; i++) {
            System.out.print("请输入备注" + (i+1) + "标题(只输⼊回⻋完成备注新增)：");
            String key = scanner.nextLine();
            if (key.isEmpty()) {
                break;
            }

            System.out.print("请输入备注内容:");
            String value = scanner.nextLine();
            builder.putRemark(key, value);
        }

        System.out.println("-----------添加联系人结束-------------");
        return builder.build();
    }
}
